//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------


namespace Microsoft.Samples.MessagingWithTopics
{
    using System;
    using Microsoft.ServiceBus;
    using Microsoft.ServiceBus.Messaging;

    public class Receiver
    {
        private static string ServiceNamespace;
        private static string IssuerName;
        private static string IssuerKey;

        static void Main(string[] args)
        {
            GetUserCredentials();
            TokenProvider tokenProvider =
                TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty);

            //*****************************************************************************************************
            //                                   Runtime Operations
            //*****************************************************************************************************

            MessagingFactory factory = MessagingFactory.Create(serviceUri, tokenProvider);

            SubscriptionClient agentSubscriptionClient = factory.CreateSubscriptionClient("IssueTrackingTopic", "AgentSubscription", ReceiveMode.PeekLock);
            SubscriptionClient auditSubscriptionClient = factory.CreateSubscriptionClient("IssueTrackingTopic", "AuditSubscription", ReceiveMode.ReceiveAndDelete);

            //*****************************************************************************************************
            //                                   Receiving messages from a Subscription
            //*****************************************************************************************************

            BrokeredMessage message;

            while ((message = agentSubscriptionClient.Receive(TimeSpan.FromSeconds(5))) != null)
            {
                Console.WriteLine("\nReceiving message from AgentSubscription...");
                Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                // Further custom message processing could go here...
                message.Complete();
            }          

            // Create a receiver using ReceiveAndDelete mode
            while ((message = auditSubscriptionClient.Receive(TimeSpan.FromSeconds(5))) != null)
            {
                Console.WriteLine("\nReceiving message from AuditSubscription...");
                Console.WriteLine(string.Format("Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
                // Further custom message processing could go here...
            }                

            Console.WriteLine("\nEnd of scenario, press ENTER to exit.");
            Console.ReadLine();

            agentSubscriptionClient.Close();
            auditSubscriptionClient.Close();
        }

        static void GetUserCredentials()
        {
            Console.Write("Please provide the service namespace to use: ");
            ServiceNamespace = Console.ReadLine();

            Console.Write("Please provide the issuer name to use: ");
            IssuerName = Console.ReadLine();

            Console.Write("Please provide the issuer key to use: ");
            IssuerKey = Console.ReadLine();
        }
    }
}
